package com.hsh.erp.init;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hsh.erp.domain.dto.*;
import com.hsh.erp.domain.entity.*;
import com.hsh.erp.enums.InOutTypeEnum;
import com.hsh.erp.mapper.*;
import com.hsh.erp.utils.MyBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class DataInitializer implements CommandLineRunner {

    @Resource
    private InOutTypeMapper inOutTypeMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private RepositoryMapper repositoryMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private SupplierMapper supplierMapper;
    @Resource
    private ExpressMapper expressMapper;

    /**
     * 进出场类型 map(id, name)
     */
    private static Map<Integer, String> inOutTypeMap;
    /**
     * 入场类型 list
     */
    private static List<InOutTypeDTO> inTypeList;
    /**
     * 出场类型 list
     */
    private static List<InOutTypeDTO> outTypeList;


    /**
     * 产品 list
     */
    private static List<ProductDTO> productDTOList;
    /**
     * 产品 map(id, name)
     */
    private static Map<Integer, String> productMap;


    /**
     * 仓库 list
     */
    private static List<RepositoryDTO> repositoryDTOList;
    /**
     * 仓库 map(id, name)
     */
    private static Map<Integer, String> repositoryMap;
    /**
     * 默认仓库id
     */
    private static Integer defaultRepositoryId;


    /**
     * 店铺 list
     */
    private static List<ShopDTO> shopDTOList;
    /**
     * 店铺 map(id, name)
     */
    private static Map<Integer, String> shopMap;
    /**
     * 店铺简称 map(simpleName, id)
     */
    private static Map<String, Integer> simpleShopMap;


    /**
     * 供应商 list
     */
    private static List<SupplierDTO> supplierDTOList;
    /**
     * 供应商 map(id, name)
     */
    private static Map<Integer, String> supplierMap;


    /**
     * 快递 list
     */
    private static List<ExpressDTO> expressDTOList;
    /**
     * 快递 map(simpleName, id)
     */
    private static Map<String, Integer> simpleExpressMap;

    /**
     * Callback used to run the bean.
     *
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    @Override
    public void run(String... args) throws Exception {

        // 预加载所有未删除的的出入库类型
        initInOutType();

        // 预加载所有未删除的商品信息,并合并所有仓库的库存
        initProduct();

        // 预加载所有未删除的仓库信息
        initRepository();

        // 预加载所有未删除的店铺信息
        initShop();

        // 预加载所有未删除的供应商信息
        initSupplier();

        // 预加载所有未删除的供应商信息
        initExpress();

    }


    /**
     * 预加载所有未删除的的出入库类型
     */
    public Map<Integer, String> initInOutType() {
        initInOutType(null);
        return inOutTypeMap;
    }


    /**
     * @param inOrOut 1-入库 2-出库
     * @return
     */
    public List<InOutTypeDTO> initInOutType(Integer inOrOut) {
        List<InOutType> inOutTypeList = InOutTypeEnum.getAll();

        // 进出场类型 map
        inOutTypeMap = inOutTypeList.stream().collect(Collectors.toMap(InOutType::getId, InOutType::getName));
        inOutTypeMap = inOutTypeMap != null ? inOutTypeMap : new HashMap<>();

        // 入场类型map初始化
        initInType(inOutTypeList);
        // 出场类型map初始化
        initOutType(inOutTypeList);

        List<InOutTypeDTO> inOutTypeDTOList = new ArrayList<>();
        if (inOrOut != null && inOrOut == 1) {// 查询入场类型
            inOutTypeDTOList = inTypeList;
        } else if (inOrOut != null && inOrOut == 2) {// 查询出场类型
            inOutTypeDTOList = outTypeList;
        }
        return inOutTypeDTOList;
    }

    /**
     * 预加载所有未删除的的出库类型
     *
     * @param inOutTypeList
     */
    private void initOutType(List<InOutType> inOutTypeList) {
        inOutTypeList = inOutTypeList.stream()
                .filter(v -> v.getInOrOut() != null && v.getInOrOut() == 1).collect(Collectors.toList());

        inTypeList = MyBeanUtils.copyList(inOutTypeList, InOutTypeDTO.class);
    }

    /**
     * 预加载所有未删除的的入库类型
     *
     * @param inOutTypeList
     */
    private void initInType(List<InOutType> inOutTypeList) {
        inOutTypeList = inOutTypeList.stream()
                .filter(v -> v.getInOrOut() != null && v.getInOrOut() == 2).collect(Collectors.toList());

        outTypeList = MyBeanUtils.copyList(inOutTypeList, InOutTypeDTO.class);
    }

    /**
     * 预加载所有未删除的商品信息
     */
    public Map<Integer, String> initProduct() {
        // 所有未删除的商品
        List<Product> productList = productMapper.selectList(new QueryWrapper<Product>().eq("is_delete", 0));
        // 商品list
        productDTOList = MyBeanUtils.copyList(productList, ProductDTO.class);
        // 商品map（id，name）
        productMap = productDTOList.stream().collect(Collectors.toMap(ProductDTO::getId, ProductDTO::getName));

        return productMap;
    }

    /**
     * 预加载所有未删除的仓库信息
     */
    public Map<Integer, String> initRepository() {
        List<Repository> repositoryList = repositoryMapper.selectList(new QueryWrapper<Repository>().eq("is_delete", 0));
        // 仓库list
        repositoryDTOList = MyBeanUtils.copyList(repositoryList, RepositoryDTO.class);
        // 仓库map（id，name）
        repositoryMap = repositoryList.stream().collect(Collectors.toMap(Repository::getId, Repository::getName));
        // 默认仓库id
        for (Repository repository : repositoryList) {
            if (repository.getIsDefault() == 1) {
                defaultRepositoryId = repository.getId();
                log.info("默认仓库id->{}", defaultRepositoryId);
                break;
            }
        }
        return repositoryMap;
    }

    /**
     * 预加载所有未删除的店铺信息
     */
    public Map<Integer, String> initShop() {
        List<Shop> shopList = shopMapper.selectList(new QueryWrapper<Shop>().eq("is_delete", 0));
        // 店铺list
        shopDTOList = MyBeanUtils.copyList(shopList, ShopDTO.class);
        // 店铺map（id，name）
        shopMap = shopList.stream().collect(Collectors.toMap(Shop::getId, Shop::getName));
        // 店铺map（simpleName，id）
        simpleShopMap = shopList.stream().filter(v -> Strings.isNotBlank(v.getSimpleName()))
                .collect(Collectors.toMap(Shop::getSimpleName, Shop::getId));

        return shopMap;
    }

    /**
     * 预加载所有未删除的供应商信息
     */
    public Map<Integer, String> initSupplier() {
        List<Supplier> supplierList = supplierMapper.selectList(new QueryWrapper<Supplier>().eq("is_delete", 0));
        // 供应商list
        supplierDTOList = MyBeanUtils.copyList(supplierList, SupplierDTO.class);
        // 供应商map（id，name）
        supplierMap = supplierList.stream().collect(Collectors.toMap(Supplier::getId, Supplier::getName));

        return supplierMap;
    }


    /**
     * 预加载所有未删除的快递信息
     */

    public Map<String, Integer> initExpress() {
        List<Express> expressList = expressMapper.selectList(new QueryWrapper<Express>().eq("is_delete", 0));
        // 快递list
        expressDTOList = MyBeanUtils.copyList(expressList, ExpressDTO.class);
        // 快递map（simpleName，id）
        simpleExpressMap = expressList.stream().filter(v -> Strings.isNotBlank(v.getSimpleName()))
                .collect(Collectors.toMap(Express::getSimpleName, Express::getId));

        return simpleExpressMap;

    }


    public static Map<Integer, String> getInOutTypeMap() {
        return inOutTypeMap;
    }

    public static List<InOutTypeDTO> getInTypeList() {
        return inTypeList;
    }

    public static List<InOutTypeDTO> getOutTypeList() {
        return outTypeList;
    }

    public static List<ProductDTO> getProductDTOList() {
        return productDTOList;
    }

    public static Map<Integer, String> getProductMap() {
        return productMap;
    }

    public static List<RepositoryDTO> getRepositoryDTOList() {
        return repositoryDTOList;
    }

    public static Map<Integer, String> getRepositoryMap() {
        return repositoryMap;
    }

    public static Integer getDefaultRepositoryId() {
        return defaultRepositoryId;
    }

    public static List<ShopDTO> getShopDTOList() {
        return shopDTOList;
    }

    public static Map<Integer, String> getShopMap() {
        return shopMap;
    }

    public static Map<String, Integer> getSimpleShopMap() {
        return simpleShopMap;
    }

    public static List<SupplierDTO> getSupplierDTOList() {
        return supplierDTOList;
    }

    public static Map<Integer, String> getSupplierMap() {
        return supplierMap;
    }

    public static List<ExpressDTO> getExpressDTOList() {
        return expressDTOList;
    }

    public static Map<String, Integer> getSimpleExpressMap() {
        return simpleExpressMap;
    }
}
